﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class my_add_photo : System.Web.UI.Page
{
    int no;
    int cno;

    protected void Page_Load(object sender, EventArgs e)
    {
        no = int.Parse(Request["no"]);
        cno = int.Parse(Request["cno"]);


    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        ctpDataContext db = new ctpDataContext();

        for (int i = 0; i < 5; i++)
        {
            FileUpload file = (FileUpload)Panel1.FindControl("FileUpload" + i.ToString());

            if (file.HasFile == true)
            {
                System.IO.FileInfo fi = new System.IO.FileInfo(file.PostedFile.FileName);
                string ext = fi.Extension.ToLower();
                if (ext == ".jpeg") ext = ".jpg";
                if (ext != ".gif" && ext != ".jpg")
                {
                    this.Label1.Text = "上傳檔案類型錯誤";
                    return;
                }

                string filename = no.ToString() + "-" + cno.ToString() + "-" + db.ct_s_pics.Count(p => p.ct_s_no == no).ToString() + ext;
                this.Label1.Text = filename;

                ct_s_pic p1 = new ct_s_pic();
                p1.create_user_no = no;
                p1.ct_s_no = cno;
                p1.language_no = (int)Session["now_user_lang_no"];
                p1.filename = filename;
                db.ct_s_pics.InsertOnSubmit(p1);

                String path = Page.MapPath("~/city_imgs/");

                ctp_lib.savepic(this.Page, filename, file.PostedFile, path);

                var cts = from p in db.ct_s_profiles
                          where p.no == cno
                          select p;

                foreach (ct_s_profile c in cts)
                {
                    if (c.main_pic_fn == null)
                    {
                        c.main_pic_fn = filename;
                    }

                }

                db.SubmitChanges();
            }
        }   
        String backurl = "~/my/index.aspx?no=" + no + "&cno=" + cno;
        Response.Redirect(backurl);
    }
}
